package project.Dao;


import project.Entity.Code;
import project.Entity.User;
import project.util.DbConn;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("SqlResolve")
public class UserDao {
//    创建JdbcTemplate对象用于执行sql语句
    private Connection conn = DbConn.getConn();

    /////更新
    public boolean updateUser(User user){
        System.out.println("正在更新用户信息");
        String sql = "update user_file set name=?,password=?,profile_url=? where email=?";
        int a = 0;
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getName());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getUrl());
            pstmt.setString(4, user.getEmail());
            a = pstmt.executeUpdate();
            pstmt.close();
            System.out.println("操作成功！已关闭数据库连接。");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return a > 0;
    }
    //////注册
    public boolean addUser(User user){
        System.out.println("正在注册新用户");
        String sql = "insert into user_file(email, password, name, profile_url, register_time) values(?,?,?,?,?)";
        int a = 0;
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getEmail());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getName());
            pstmt.setString(4, user.getUrl());
            pstmt.setString(5, user.getTime());
            a = pstmt.executeUpdate();
            pstmt.close();
            System.out.println("操作成功！已关闭数据库连接。");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return a > 0;
    }
    ////邮箱找回密码
    public List<User> getUserByEmail(String em){
        System.out.println("正在查询用户");
        List<User> list = new ArrayList<>();
        String sql = "select * from user_file";
        if(!em.equals("all"))   sql = sql + " where email = ? ";
//        将查询的结果封装为一个userList集合返回
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement(sql);
            if(!em.equals("all"))   pstmt.setString(1, em);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                String email = rs.getString("email");
                String password = rs.getString("password");
                String name = rs.getString("name");
                String profile_url = rs.getString("profile_url");
                String register_time = rs.getString("register_time");
                User user = new User(email, password, name, profile_url, register_time);
                list.add(user);
            }
            pstmt.close();
            System.out.println("操作成功！已关闭数据库连接。");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    ////删除临时验证码
    public boolean deleteTempCode(String email){
        System.out.println("删除临时验证码");
        String sql = "delete from temp_code where email=?";
        int a = 0;
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, email);
            a= pstmt.executeUpdate();
            pstmt.close();
            System.out.println("操作成功！已关闭数据库连接。");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return a > 0;
    }
    ////查询是否已存在临时验证码
    public List<Code> queryTempCode(String email){
        System.out.println("正在查询/获取临时验证码");
        List<Code> list = new ArrayList<>();
        String sql = "select * from temp_code where email = ? ";
//        将查询的结果封装为一个List集合返回
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, email);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                String mail = rs.getString("email");
                String code = rs.getString("code");
                Code c = new Code(mail, code);
                list.add(c);
            }
            pstmt.close();
            System.out.println("操作成功！已关闭数据库连接。");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    ////放置临时验证码
    public boolean saveTempCode(String email,String code){
        System.out.println("正在保存 临时验证码");
        String sql = "insert into temp_code(email, code) values(?,?)";
        int a = 0;
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, email);
            pstmt.setString(2, code);
            a = pstmt.executeUpdate();
            pstmt.close();
            System.out.println("操作成功！已关闭数据库连接。");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return a > 0;
    }
    ////更新验证码
    public boolean updateTempCode(String email,String code){
        System.out.println("正在更新 临时验证码");
        String sql = "update temp_code set code=? where email=?";
        int a = 0;
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, code);
            pstmt.setString(2, email);
            a = pstmt.executeUpdate();
            pstmt.close();
            System.out.println("操作成功！已关闭数据库连接。");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return a > 0;
    }
}
